package com.fz.song.exportexcel;

import cn.binarywang.tools.generator.ChineseMobileNumberGenerator;
import cn.binarywang.tools.generator.ChineseNameGenerator;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.RandomUtil;
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.write.metadata.WriteSheet;
import com.alibaba.excel.write.metadata.fill.FillConfig;
import lombok.Data;
import org.springframework.core.io.ClassPathResource;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.*;

@RestController
public class Hi {


    @GetMapping("export")
    public void export(HttpServletResponse response) throws IOException {
        List<Student> list = getData();
        ArrayList<Map<String, Object>> maps = new ArrayList<>();
        Integer should = 0;
        Integer actual = 0;
        for (Student student : list) {
            should += student.getShould();
            actual += student.getActual();

            Map<String, Object> map = BeanUtil.beanToMap(student);
            maps.add(map);
        }

        Map<String, Object> params = new HashMap<>();
        params.put("should", should);
        params.put("actual", actual);


        ClassPathResource classPathResource = new ClassPathResource("templates/xueshi.xlsx");

        String name =  "xxx校区课时统计.xlsx";
        //获取response
        this.getHttpServletResponse(response, name);
        //获取模板
        try {
            InputStream inputStream = classPathResource.getInputStream();
            ServletOutputStream outputStream = response.getOutputStream();
            //设置输出流和模板信息
            ExcelWriter excelWriter = EasyExcel.write(outputStream).withTemplate(inputStream).build();
            WriteSheet sheet = EasyExcel.writerSheet().build();
            //开启自动换行,自动换行表示每次写入一条list数据是都会重新生成一行空行,此选项默认是关闭的,需要提前设置为true
            FillConfig config = FillConfig.builder().forceNewRow(Boolean.TRUE).build();
            excelWriter.fill(maps, config, sheet);
            //list 要放最后
            excelWriter.fill(params, sheet);
            excelWriter.finish();
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    private HttpServletResponse getHttpServletResponse(HttpServletResponse response, String fileName) {
        response.setContentType("application/octet-stream");
        response.setHeader("content-type", "application/octet-stream");
        response.setCharacterEncoding("utf-8");
        // 这里URLEncoder.encode可以防止中文乱码
        try {
            fileName = URLEncoder.encode(fileName, "UTF-8").replaceAll("\\+", "%20");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        response.setHeader("Content-disposition", "attachment;filename=" + fileName);
        return response;
    }


    public List<Student> getData() {
        ArrayList<Student> list = new ArrayList<>();
        for (int i = 0; i < 15; i++) {
            Student student = new Student();
            student.setName(ChineseNameGenerator.getInstance().generate());
            student.setMobile(ChineseMobileNumberGenerator.getInstance().generate());
            student.setCourse(getCourse());
            student.setClasses(getClasses());
            student.setWay(i % 2 == 0 ? "一对一" : "一对多");
            student.setTeacher(getTeacher());
            student.setState(i % 2 == 0 ? "到课" : "请假");
            int num = RandomUtil.randomInt(1, 3);
            student.setShould(num);
            student.setActual(RandomUtil.randomInt(num));
            list.add(student);
        }
        return list;
    }


    public String getCourse() {
        String[] course = {"语文", "数学", "英语", "政治", "历史", "地里", "物理", "化学", "生物"};
        int i = RandomUtil.randomInt(0, 9);
        return course[i];
    }

    public String getClasses() {
        String[] classes = {"一班", "二班", "三班"};
        int i = RandomUtil.randomInt(0, 3);
        return classes[i];
    }

    public String getTeacher() {
        String[] classes = {"张老师", "李老师", "赵老师"};
        int i = RandomUtil.randomInt(0, 3);
        return classes[i];
    }


    @Data
    public static class Student {
        private String name;
        private String mobile;
        private String course;
        private String classes;
        private String way;
        private String teacher;
        private String state;
        private Integer should;
        private Integer actual;
    }
}
